﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace DAL
{
    public class StudentDal
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string constr = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
        /// <summary>
        /// 数据显示
        /// </summary>
        /// <returns></returns>
        public List<Student> Getshow()
        {
            List<Student> list = new List<Student>();
            using (SqlConnection conn=new SqlConnection(constr))
            {
                conn.Open();//打开数据库
                SqlCommand cmd = new SqlCommand("select * from Student", conn);
                SqlDataAdapter dap = new SqlDataAdapter(cmd);
                DataTable dt = new System.Data.DataTable();
                dap.Fill(dt);
                foreach (DataRow item in dt.Rows)
                {
                    Student mod = new Student();
                    mod.Id = Convert.ToInt32(item["Id"]);
                    mod.Name = item["Name"].ToString();
                    mod.Sex = item["Sex"].ToString();
                    mod.Age = Convert.ToInt32(item["Age"]);
                    list.Add(mod);
                }
                return list;
                
            }
        }
        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        public int Add(Student mod)
        {
            using (SqlConnection conn=new SqlConnection(constr))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Student VALUES('"+mod.Name+"','"+mod.Sex+"','"+mod.Age+"')", conn);
                int i = cmd.ExecuteNonQuery();
                return i;
            }
        }
    }
}

